package com.healthcloud.controller.modules.patient;

import cn.hutool.core.util.StrUtil;
import com.healthcloud.controller.modules.common.BaseController;
import com.healthcloud.entity.BoneLoose;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.PpPatientBaseEntity;
import com.healthcloud.entity.patient.*;
import com.healthcloud.entity.common.PageList;
import com.healthcloud.service.BoneLooseService;
import com.healthcloud.service.PatientRecord.PatientBoneLooseService;
import com.healthcloud.service.PpPatientBaseService;
import com.healthcloud.service.patient.BoneLooseResultService;
import com.healthcloud.service.patient.BoneLooseCheckService;
import com.healthcloud.service.patient.BoneLooseDrugService;
import com.healthcloud.service.patient.BoneLooseScoreService;
import com.healthcloud.utils.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 骨松
 */
@Controller
@RequestMapping("boneLoose")
public class BoneLooseController extends BaseController {

    @Autowired
    private BoneLooseService boneLooseService;

    @Autowired
    private PatientBoneLooseService patientBoneLooseService;

    @Autowired
    private PpPatientBaseService ppPatientBaseService;

    @Autowired
    private BoneLooseCheckService boneLooseCheckService;

    @Autowired
    private BoneLooseResultService boneLooseResultService;

    @Autowired
    private BoneLooseScoreService boneLooseScoreService;

    @Autowired
    private BoneLooseDrugService boneLooseDrugService;

    @ApiOperation(value = "病人列表")
    @RequestMapping(value = "/patients", method = RequestMethod.GET)
    public String patients() {
        setAttr();
        return "bus/boneLoose/patients";
    }

    @ApiOperation(value = "病人基本信息")
    @RequestMapping(value = "/patient", method = RequestMethod.GET)
    public String patient() {
        setAttr();
        return "bus/boneLoose/patient";
    }

    @ApiOperation(value = "医疗服务(钱湖)病人列表")
    @RequestMapping(value = "/qianhu/blPatients", method = RequestMethod.GET)
    public String blQHPatients() {
        setAttr();
        return "bus/boneLoose/qianhu/blPatients";
    }

    @ApiOperation(value = "医疗服务(钱湖)病人详情页")
    @RequestMapping(value = "/qianhu/blDetails", method = RequestMethod.GET)
    public String blQHDetails() {
        setAttr();
        return "bus/accompany/blAccompanysService";
    }

    @ApiOperation(value = "诊疗病人列表页")
    @RequestMapping(value = "/checkPatients", method = RequestMethod.GET)
    public String checkPatients() {
        setAttr();
        return "bus/boneLoose/checkPatients";
    }

    @ApiOperation(value = "诊疗列表页")
    @RequestMapping(value = "/checks", method = RequestMethod.GET)
    public String checks() {
        setAttr();
        return "bus/boneLoose/checks";
    }

    @ApiOperation(value = "诊疗页")
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public String check() {
        setAttr();
        return "bus/boneLoose/check";
    }

    @ApiOperation(value = "诊疗BMD修改页")
    @RequestMapping(value = "/checkBMD", method = RequestMethod.GET)
    public String checkBMD() {
        setAttr();
        return "bus/boneLoose/checkBMD";
    }

    @ApiOperation(value = "诊疗T-score修改页")
    @RequestMapping(value = "/checkT", method = RequestMethod.GET)
    public String checkT() {
        setAttr();
        return "bus/boneLoose/checkT";
    }

    @ApiOperation(value = "诊疗用药页面")
    @RequestMapping(value = "/checkDrug", method = RequestMethod.GET)
    public String checkDrug() {
        setAttr();
        return "bus/boneLoose/checkDrug";
    }

    @ApiOperation(value = "诊疗提醒列表页")
    @RequestMapping(value = "/checkReminds", method = RequestMethod.GET)
    public String checkReminds() {
        setAttr();
        return "bus/boneLoose/checkReminds";
    }

    @ApiOperation(value = "诊疗列表")
    @RequestMapping(value = "/checkList", method = RequestMethod.GET)
    @ResponseBody
    public MsgVo checkList(BoneLooseCheckVo entity, @ModelAttribute("pageParam") PageParam pageParam) {
        List<BoneLooseCheckEntity> list = boneLooseCheckService.queryList(entity, pageParam);
        int count = boneLooseCheckService.queryTotal(entity);
        return new MsgVo(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "诊疗数据")
    @RequestMapping(value = "/checkInfo", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes checkInfo(Integer id) {
        BoneLooseCheckEntity entity = boneLooseCheckService.queryObject(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "获取骨松患者的最近一次诊疗数据")
    @RequestMapping(value = "/getLatestCheckInfo", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes getLatestCheckInfo(String idCard) {
        if (StrUtil.isBlank(idCard)) {
            return ApiRes.failure("参数有误。");
        }
        BoneLooseCheckEntity entity = boneLooseCheckService.getLatestCheckInfo(idCard);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "保存诊疗")
    @RequestMapping(value = "/saveCheck", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes save(@RequestBody BoneLooseCheckDto entity) {
        boneLooseCheckService.saveOrUpdate(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除诊疗列表")
    @RequestMapping(value = "/deleteCheck", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes delete(Integer[] ids) {
        boneLooseCheckService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "骨松列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public PageList bsList(BoneLoose entity, PageParam pageParam) {
        List<BoneLoose> list = boneLooseService.queryList(entity, pageParam);
        Integer count = boneLooseService.queryTotal(entity);
        return new PageList(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "骨松result列表")
    @RequestMapping(value = "/resultList", method = RequestMethod.GET)
    @ResponseBody
    public PageList resultList(BoneLooseResultEntity entity, PageParam pageParam) {
        List<BoneLooseResultEntity> list = boneLooseResultService.queryList(entity, pageParam);
        Integer count = boneLooseResultService.queryTotal(entity);
        return new PageList(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "骨松score列表")
    @RequestMapping(value = "/scoreList", method = RequestMethod.GET)
    @ResponseBody
    public PageList scoreList(BoneLooseScoreEntity entity, PageParam pageParam) {
        List<BoneLooseScoreEntity> list = boneLooseScoreService.queryList(entity, pageParam);
        Integer count = boneLooseScoreService.queryTotal(entity);
        return new PageList(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "骨松用药列表")
    @RequestMapping(value = "/drugList", method = RequestMethod.GET)
    @ResponseBody
    public PageList drugList(BoneLooseDrugVo entity, PageParam pageParam) {
        List<BoneLooseDrugEntity> list = boneLooseDrugService.queryList(entity, pageParam);
        Integer count = boneLooseDrugService.queryTotal(entity);
        return new PageList(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "跳转到新增页面")
    @RequestMapping(value = "/toSave", method = RequestMethod.GET)
    public String toSave() {
        setAttr();
        return "bus/boneLoose/addOrUpdateBoneLoose";
    }

    @ApiOperation(value = "保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes save(BoneLoose entity) {
        BoneLoose boneLoose = boneLooseService.queryOne(entity);
        if (boneLoose != null) {
            entity.setId(boneLoose.getId());
        }
        boneLooseService.saveOrUpdate(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存result")
    @RequestMapping(value = "/resultSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveResult(BoneLooseResultEntity entity) {
        BoneLooseResultEntity result = boneLooseResultService.queryOne(entity);
        if (result != null) {
            entity.setId(result.getId());
        }
        boneLooseResultService.saveOrUpdate(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存score")
    @RequestMapping(value = "/scoreSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveScore(BoneLooseScoreEntity entity) {
        BoneLooseScoreEntity score = boneLooseScoreService.queryOne(entity);
        if (score != null) {
            entity.setId(score.getId());
        }
        boneLooseScoreService.saveOrUpdate(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存用药")
    @RequestMapping(value = "/drugSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveDrug(BoneLooseDrugEntity entity) {
        boneLooseDrugService.saveOrUpdate(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存骨松基本档案")
    @RequestMapping(value = "/patientBoneLooseSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes patientBoneLooseSave(PatientBoneLooseEntity entity) {

        PatientBoneLooseEntity oldEntity = patientBoneLooseService.findByIdCard(entity.getIdCard());
        if (oldEntity == null) {
            patientBoneLooseService.save(entity);
        } else {
            entity.setId(oldEntity.getId());
            patientBoneLooseService.update(entity);
        }
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "跳转骨松基本档案修改页面")
    @RequestMapping(value = "/patientBoneLooseToSave", method = RequestMethod.GET)
    public String patientBoneLooseToSave() {
        return "bus/boneLoose/addOrUpdatePatientboneloose";
    }

    @ApiOperation(value = "获取骨松基本档案信息")
    @RequestMapping(value = "/patientBoneLoose", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes patientBoneLoose(String idCard) {
        PatientBoneLooseEntity entity = patientBoneLooseService.findByIdCard(idCard);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "用药信息")
    @RequestMapping(value = "/drug", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes drug(Integer id) {
        BoneLooseDrugEntity entity = boneLooseDrugService.queryObject(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "删除用药")
    @RequestMapping(value = "/drugDelete", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes drugDelete(Integer[] ids) {
        boneLooseDrugService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "获取osta")
    @RequestMapping(value = "/getOsta", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes getOsta(String idCard) {
        PpPatientBaseEntity entity = ppPatientBaseService.queryByIdcard(idCard);
        Map data = new HashMap();
        Double o = 0.2 * (entity.getWeight() - (new IdcardInfoExtractorUtil(idCard)).getAge());
        data.put("riskLevel", "中");
        data.put("osta", "-1~-4");
        if (o > -1) {
            data.put("riskLevel", "低");
            data.put("osta", ">-1");
        }
        if (o < -4) {
            data.put("riskLevel", "高");
            data.put("osta", "<-4");
        }
        return new ApiRes(data);
    }


    @ApiOperation(value = "跳转骨松基本档案(钱湖)详情页面")
    @RequestMapping(value = "/qianhu/detail", method = RequestMethod.GET)
    public String detail() {
        return "bus/boneLoose/qianhu/detail";
    }

}
